package com.atguigu.flink.streamapi.exec;

import com.alibaba.fastjson.JSON;
import com.atguigu.flink.pojo.ChannelData;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * Created by Smexy on 2022/11/22
 */
public class Demo3_Channel
{
    public static void main(String[] args) {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // { userId:100,behavior:"uninstall","channel":"ZTE",timestamp:xxx  }
        env.addSource(new MySource()).setParallelism(1)
           .map(new MapFunction<String, Tuple2<String ,Integer>>()
           {
               @Override
               public Tuple2<String ,Integer> map(String value) throws Exception {
                   ChannelData channelData = JSON.parseObject(value, ChannelData.class);
                   return Tuple2.of(channelData.getChannel()+"_"+channelData.getBehavior(),1);
               }
           })
           .keyBy(t -> t.f0)
           .sum(1)
           .print();


        try {
                    env.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    }
}
